﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using common;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class MaintenControl : UserControl
    {
        DataPaging MaintenPage;
        DataSet MaintenDataSet;
        Util util = new Util();
        MaintenBI maintenBI = new MaintenBI();
        int pagecount = 18;
        dbOperate dbo = new dbOperate();
        public MaintenControl()
        {
            InitializeComponent();
        }

        private void MaintenControcs_Load(object sender, EventArgs e)
        {
            RBAll.Checked = true;
            this.dateTimePicker1.Enabled = false;
            this.dateTimePicker2.Enabled = false;      
            if (this.Btn_search.Image != null)
            {
                Btn_search.Image = new Bitmap(Btn_search.Image, Btn_search.Height - 4, Btn_search.Height - 4);
            }
            MaintenPage = maintenBI.GetMaintenpage(pagecount);
            InitData();
            maintenGridView.ReadOnly = true;

            //查找所有分公司作为数据源
         
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0]="0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
            TBcompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBcompany.DataSource = companyTable;
            this.TBcompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBcompany.ValueMember = "ID";//标识
         /*   //查找分公司有的维修人员作为数据源
            DataTable maintorTable = maintenBI.Getmaintorbyscid(TBcompany.SelectedValue.ToString());
            DataRow maintordr = maintorTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            maintorTable.Rows.InsertAt(maintordr, 0);
            TBmaintor.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBmaintor.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBmaintor.DataSource = maintorTable;
            this.TBmaintor.DisplayMember = "maintorName";//客户所看到的
            this.TBmaintor.ValueMember = "ID";//标识
          
           //查找所有客户作为数据源
            DataTable clientTable = maintenBI.Getallclient();
            DataRow clientdr = clientTable.NewRow();
            clientdr[0] = "0";
            clientdr[1] = "";
            clientTable.Rows.InsertAt(clientdr, 0);
            TBclient.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBclient.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBclient.DataSource = clientTable;
            this.TBclient.DisplayMember = "ClientName";//客户所看到的
            this.TBclient.ValueMember = "ID";//标识*/
           
        }
        private void InitData()
        {

           // MaintenPage = maintenBI.GetMaintenpage(pagecount);
            MaintenDataSet = MaintenPage.Homepage();
            int x = maintenBI.GetDatacount(MaintenPage);
            label1.Text = "总共" + maintenBI.GetDatacount(MaintenPage) + "条记录，当前第" + MaintenPage.GetPage() + "页，每页" + pagecount + "条";
            maintenGridView.DataSource = MaintenDataSet;
            maintenGridView.DataMember = "data";
            // this.dataGridViewMaintenType.Columns[e.ColumnIndex] is DataGridViewButtonColumn
          /*  for(int i=0;i<maintenGridView.Rows.Count;i++)
            {
                dataGridView1.Rows[i].Cells["statusDataGridViewCheckBoxColumn"] = new DataGridViewTextBoxCell();
              
            }*/
      
          
        }
    
            
        private void btn_mainpage_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void maintenGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        /*
         * 条件查询
         * */
        private void Btn_search_Click(object sender, EventArgs e)
        {
            
            MaintenPage = maintenBI.fuzzysearch(pagecount,fuzzytextbox.Text.Trim());
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0] = "0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
         //   TBcompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBcompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBcompany.DataSource = companyTable;
            this.TBcompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBcompany.ValueMember = "ID";//标识
            DataTable maintorTable = maintenBI.Getmaintorbyscid(TBcompany.SelectedValue.ToString());
            DataRow maintordr = maintorTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            maintorTable.Rows.InsertAt(maintordr, 0);
          //  TBmaintor.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBmaintor.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBmaintor.DataSource = maintorTable;
            this.TBmaintor.DisplayMember = "maintorName";//客户所看到的
            this.TBmaintor.ValueMember = "ID";//标识
           /* MaintenPage = maintenBI.Searchmainten(pagecount, TBclient.Text, Mcontextbox.Text, TBcompany.Text,TBmaintor.Text,begindate, enddate);*/
            InitData();
        }
        /*
         * 公司变化时维修人员相应的变化
         * */
        private void TBcompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (TBcompany.SelectedValue != null && TBcompany.SelectedValue.ToString() != "0")
            {
                DataTable maintorTable = maintenBI.Getmaintorbyscid(TBcompany.SelectedValue.ToString());
                DataRow maintordr = maintorTable.NewRow();
                maintordr[0] = "0";
                maintordr[1] = "";
                maintorTable.Rows.InsertAt(maintordr, 0);
               // TBmaintor.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
                TBmaintor.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.TBmaintor.DataSource = maintorTable;
                this.TBmaintor.DisplayMember = "maintorName";//客户所看到的
                this.TBmaintor.ValueMember = "ID";//标识
                MaintenPage = maintenBI.SearchBycompany(pagecount, fuzzytextbox.Text.Trim(), TBcompany.SelectedValue.ToString());
                InitData();
            }
            else
            {
                this.TBmaintor.DataSource = null;
                MaintenPage = maintenBI.fuzzysearch(pagecount, fuzzytextbox.Text.Trim());
                InitData();
            }
        }
        private void TBmaintor_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (TBmaintor.SelectedValue != null && TBmaintor.SelectedValue.ToString() != "0")
            {
                string companyid = "0";
                
                if (TBcompany.SelectedValue != null)
                {
                    companyid = TBcompany.SelectedValue.ToString();
                }
                MaintenPage = maintenBI.SearchBymaintorname(pagecount, fuzzytextbox.Text.Trim(), TBmaintor.Text.ToString(),companyid);
                InitData();
            }
            else
            {
               
                MaintenPage = maintenBI.SearchBycompany(pagecount, fuzzytextbox.Text.Trim(), TBcompany.SelectedValue.ToString());
                InitData();
            }
        }
      
       
        /*
         * 上一页
         * */
        private void btn_lastpage_Click(object sender, EventArgs e)
        {
            if (MaintenPage.GetPage() > 1)
            {
                MaintenDataSet = MaintenPage.GetForeData();
                maintenGridView.DataSource = MaintenDataSet;
                label1.Text = "总共" + maintenBI.GetDatacount(MaintenPage) + "条记录，当前第" + MaintenPage.GetPage() + "页，每页" + pagecount + "条";
                maintenGridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是第一页");
            }
        }
        /*
         * 下一页
         * */
        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            if (MaintenPage.GetPage() < MaintenPage.GetTotalCount())
            {
                MaintenDataSet = MaintenPage.GetFollowData();
                maintenGridView.DataSource = MaintenDataSet;
                label1.Text = "总共" + maintenBI.GetDatacount(MaintenPage) + "条记录，当前第" + MaintenPage.GetPage() + "页，每页" + pagecount + "条";
                maintenGridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是最后一页");
            }
        }
        /*
         * 末页
         * */
        private void btn_endpage_Click(object sender, EventArgs e)
        {
            if (MaintenPage.GetTotalCount() == 1)
            {
                InitData();
            }
            else
            {
                MaintenDataSet = MaintenPage.TailPage();
                if (MaintenDataSet != null && MaintenDataSet.Tables["data"].Rows.Count > 0)
                {
                    maintenGridView.DataSource = MaintenDataSet;
                    label1.Text = "总共" + maintenBI.GetDatacount(MaintenPage) + "条记录，当前第" + MaintenPage.GetPage() + "页，每页" + pagecount + "条";
                    maintenGridView.DataMember = "data";
                }
            }
        }
        /*
         * 跳页
         * */
        private void btn_skippage_Click(object sender, EventArgs e)
        {
            if(util.isnumeric(textBoxX3.Text.Trim()))
            {
                int skippage=1;
                if(textBoxX3.Text.Trim()!=""&&textBoxX3.Text!=null)
                {
                  skippage = Convert.ToInt32(textBoxX3.Text.Trim());
                }
            if (skippage == 1)
            {
                InitData();
            }
            else
            {
                MaintenDataSet = MaintenPage.SkipPage(skippage);
                if (MaintenDataSet != null && MaintenDataSet.Tables["data"].Rows.Count > 0)
                {
                    maintenGridView.DataSource = MaintenDataSet;
                    label1.Text = "总共" + maintenBI.GetDatacount(MaintenPage) + "条记录，当前第" + MaintenPage.GetPage() + "页，每页" + pagecount + "条";
                    maintenGridView.DataMember = "data";
                }
            }
            }
            else
                MessageBox.Show("请输入数字再进行跳页");
        }
        /*
         * 添加维修记录
         * */
        private void Btn_addmrecord_Click(object sender, EventArgs e)
        {
            FormAddMainten addmainten = new FormAddMainten();
            addmainten.ShowDialog();
            if (addmainten.DialogResult == DialogResult.Yes)
            {
                MaintenPage = maintenBI.GetMaintenpage(pagecount);
                this.InitData();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string begindate = dateTimePicker1.Text.Replace("年","/").Replace("月","/").Replace("日","");
            string enddate = dateTimePicker2.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string companyid = "0";
            string maintorname = "";
            if (TBcompany.SelectedValue != null)
            { 
                companyid=TBcompany.SelectedValue.ToString();
            }
            if(TBmaintor.SelectedValue!=null)
            {
                maintorname = TBmaintor.Text.ToString().Trim();
            }
            if (this.dateTimePicker1.Value <= this.dateTimePicker2.Value )
            {
               MaintenPage= maintenBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyid, maintorname, begindate, enddate);
                InitData();
            }
            else
            {
                MessageBox.Show("请重新选择时间段！");
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            string begindate = dateTimePicker1.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string enddate = dateTimePicker2.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string companyid = "0";
            string maintorname = "";
            if (TBcompany.SelectedValue != null)
            {
                companyid = TBcompany.SelectedValue.ToString();
            }
            if (TBmaintor.SelectedValue != null)
            {
                maintorname = TBmaintor.Text.ToString().Trim();
            }
            if (this.dateTimePicker1.Value <= this.dateTimePicker2.Value)
            {
                MaintenPage = maintenBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyid, maintorname, begindate, enddate);
                InitData();
            }
            else
            {
                MessageBox.Show("请重新选择时间段！");
            }
        }

      

        

        private void maintenGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (maintenGridView.CurrentRow != null&&e.RowIndex>=0)
            {
                string id = maintenGridView.CurrentRow.Cells[(maintenGridView.CurrentRow.Cells.Count) - 1].Value.ToString();
                if (maintenGridView.Columns[e.ColumnIndex].Name == "dataGridViewButtonXColumn1")
                {
                    MaintenInforForm mdForm = new MaintenInforForm(Convert.ToInt32(id));
                    mdForm.Show();
                }
                else if (maintenGridView.Columns[e.ColumnIndex].Name == "Column1")
                {
                    if (maintenBI.updateifrevisitbyid(Convert.ToInt32(id)))
                    {
                        maintenGridView.Rows[e.RowIndex].Cells["Column2"].Value = "已回访";
                    }
                }
            }
        }

        private void maintenGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //导出excel
        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(maintenGridView, MaintenPage.allData(), 2);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
           
        }

        private void RBAll_CheckedChanged(object sender, EventArgs e)
        {
            string begindate;
            string enddate;
            string companyid = "0";
            string maintorname = "";
            if (TBcompany.SelectedValue != null)
            {
                companyid = TBcompany.SelectedValue.ToString();
            }
            if (TBmaintor.SelectedValue != null)
            {
                maintorname = TBmaintor.Text.ToString().Trim();
            }
            RadioButton thisRadioBtn = (RadioButton)sender;
            int tag = Convert.ToInt32(thisRadioBtn.Tag.ToString());
            switch (tag)
            {
                case 1:
                    //点击全部按钮
                    if (thisRadioBtn.Checked)
                    {
                        InitData();
                    }
                    break;

                case 2:
                    //点击近三天
                    if (thisRadioBtn.Checked)
                    {
                       // threeDays();
                       begindate = DateTime.Now.AddDays(-3).ToString("yyyy/M/d");
                       enddate = DateTime.Now.ToString("yyyy/M/d");
                        MaintenPage = maintenBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyid, maintorname, begindate, enddate);
                        InitData();
                    }
                    break;

                case 3:
                    //点击近一周
                    if (thisRadioBtn.Checked)
                    {
                       // aweek();
                        begindate = DateTime.Now.AddDays(-7).ToString("yyyy/M/d");
                        enddate = DateTime.Now.ToString("yyyy/M/d");
                        MaintenPage = maintenBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyid, maintorname, begindate, enddate);
                        InitData();
                    }
                    break;

                case 4:
                    //点击近一月
                    if (thisRadioBtn.Checked)
                    {
                     //   amonth();
                        begindate = DateTime.Now.AddDays(-30).ToString("yyyy/M/d");
                        enddate = DateTime.Now.ToString("yyyy/M/d");
                        MaintenPage = maintenBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyid, maintorname, begindate, enddate);
                        InitData();
                    }
                    break;

                case 5:
                    //点击自定义
                    if (thisRadioBtn.Checked)
                    {
                        this.dateTimePicker1.Enabled = true;
                        this.dateTimePicker2.Enabled = true;

                    }
                    else
                    {
                        this.dateTimePicker1.Enabled = false;
                        this.dateTimePicker2.Enabled = false;
                    }
                    break;
            }
        }
    }
}
